﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AdministracijaDataLayer.Repository;
using AdministracijaDataLayer;

namespace Administracija
{
    public partial class JezikBrowse : Common.Forms.Browse
    {
        RJezik rJezik = new RJezik();

        #region Refresh grid
        void RefreshGrid()
        {
            int topRowIndex = gridView1.TopRowIndex;
            int focusedRowHandle = gridView1.FocusedRowHandle;

            try
            {
                bindingSource1.DataSource = rJezik.VratiSve();
            }
            catch (Exception ex)
            {
                Common.Tools.MessageError.Show("Greška: " + ex.Message);
            }

            gridView1.FocusedRowHandle = focusedRowHandle;
            gridView1.TopRowIndex = topRowIndex;

        }
        #endregion

        public JezikBrowse()
        {
            InitializeComponent();
        }

        private void JezikBrowse_Load(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        #region Nov
        private void btnNov_Click(object sender, EventArgs e)
        {
            JezikRecView f = new JezikRecView(0);
            if (f.ShowDialog() == DialogResult.OK)
            {
                gridView1.TopRowIndex = f.Index - 6;
                RefreshGrid();
                bindingSource1.Position = f.Index;
            }
        }
        #endregion

        #region Briši
        private void btnBrisi_Click(object sender, EventArgs e)
        {
            if (gridView1.RowCount == 0)
            {
                Common.Tools.MessageError.ShowNePostojiSlogZaBrisanje();
                return;
            }

            if (MessageBox.Show("Da li želite da obrišete slog?", "Potvrda brisanja", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    rJezik.Brisi(((Jezik)bindingSource1.Current).JezikId);
                    RefreshGrid();
                }
                catch (Exception ex)
                {
                    Common.Tools.MessageError.Show(ex.Message);
                }
            }
        }
        #endregion

        #region Izmeni
        private void btnIzmeni_Click(object sender, EventArgs e)
        {
            if (gridView1.RowCount == 0)
            {
                Common.Tools.MessageError.ShowNePostojiSlogZaIzmenu();
                return;
            }

            JezikRecView f = new JezikRecView(((Jezik)bindingSource1.Current).JezikId);
            if (f.ShowDialog() == DialogResult.OK) RefreshGrid();
        }
        #endregion
    }
}
